package com.baiytfp.hf.HFDataExchange.action.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baiytfp.hf.HFDataExchange.action.dao.AuthDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.utils.CommonUtil;
import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.common.db.QueryHelper;

@Service
public class AuthService  {
	
	@Resource(name = DBManager.DB_TYPE + "AuthDAOImpl")
	private AuthDAOImpl authDAO;
	
	/**
	 * 获取指定对象的菜单权限数据
	 * @param type 对象类型
	 * @param code 对象唯一标识
	 * @return
	 */
	public List<Map> trees(String treeType,String objType,String objCode){
		return authDAO.treeDatas(treeType, objType, objCode);
	}
	
	public boolean save(List<Map> news , List<Map> edits) {
		Connection conn = null;
		try {
			conn= DBManager.getConnection();
			DBManager.beginTransaction(conn);
			
			if(!CommonUtil.isEmpty(news)) {
				authDAO.addAuth(news,conn);
			}
			
			if(!CommonUtil.isEmpty(edits)) {
				authDAO.updateAuth(edits,conn);
			}
			
			DBManager.commitTransaction(conn);
			return true;
		} catch (SQLException e) {
			DBManager.rollbackTransaction(conn);
			e.printStackTrace();
			return false;
		}finally {
			DBManager.close(conn);
		}
	}
	
	
}
